/**
 * Created by zhangyilin on 2018/12/8.
 */


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 进行中
 */

@WebServlet("/Main1")

public class Main1 extends HttpServlet {

    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库team
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        int nums = 0;
        Connection conn = GetConnection();

        conn = GetConnection();
        PreparedStatement getTaskconPS = null;
        ResultSet getTaskconRS = null;//查询获取任务内容
        String stuid =(String) req.getSession().getAttribute("ID");

        String getTaskconSql = "select count(*) from v_task_stu WHERE StudentID='"+stuid+"'";
        if(conn != null)
            try {
                getTaskconPS = conn.prepareStatement(getTaskconSql);
                if(getTaskconPS != null) {
                    getTaskconRS = getTaskconPS.executeQuery();
                    if(getTaskconRS != null && getTaskconRS.next()) {
                        String num = getTaskconRS.getString("count(*)");
                        nums = Integer.parseInt(num);
                    }

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(getTaskconRS != null)
                        getTaskconRS.close();
                    if(getTaskconPS != null)
                        getTaskconPS.close();
                    if(conn != null)
                        conn.close();
                    getTaskconRS = null;
                    getTaskconPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }




        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String time = df.format(new Date());
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>输出</title>");
        out.println("</head>");




        out.print("<meta charset=\"utf-8\" />\n" +
                "\t\t<title>雨滴特效</title>\n" +
                "\t\t<style>\n" +
                "\t\t\tbody {\n" +
                "\t\t\t\tmargin: 0;\n" +
                "\t\t\t}\n" +
                "\t\t\t\n" +
                "\t\t\tcanvas {\n" +
                "\t\t\t\tbackground-color: black;\n" +
                "\t\t\t\tdisplay: block;\n" +
                "\t\t\t}\n" +
                "\t\t\t\n" +
                "\t\t\t#ff {\n" +
                "\t\t\t\tposition: absolute;\n" +
                "\t\t\t\ttop: 30%;\n" +
                "\t\t\t\tleft: 30%;\n" +
                "\t\t\t\tz-index: 1;\n" +
                "\t\t\t\ttext-align: center;\n" +
                "\t\t\t\tcolor: white;\n" +
                "\t\t\t\tfont: \"楷体\";\n" +
                "\t\t\t}\n" +
                "\t\t\t\n" +
                "\t\t\timg {\n" +
                "\t\t\t\tposition: absolute;\n" +
                "\t\t\t\ttop: 10%;\n" +
                "\t\t\t\tleft: 20%;\n" +
                "\t\t\t\ttransform: scale(0.6);\n" +
                "\t\t\t}\n" +
                "\t\t</style><body>" +
                "<img src=\"image/welcome.png\" />\n" +
                "\n" +
                "\t\t<canvas class='rain'>\n" +
                "\t\t\t\n" +
                "\t\t</canvas>");


//            out.print("<body>");

        out.print("<div id='ff'><form action=\"changeCompleteStatus\" method=\"get\">");




//        out.println("<style>");
//        out.println("h1{text-shadow: 5px 5px 5px #FF0000;}");
//        out.println("</style>");
//        out.println("<body background=\"背景.jpg\" style=\"background-repeat: no-repeat; background-size: 100% 100%; background-attachment: fixed;\">");
        out.println("<center>");
//        out.println("<h1>欢迎您使用小程序!</h1>");
        out.println("<h3>查看结果</h3>");
        out.println("任务数量:"+nums);
//        out.println("<br>");
        //out.println("任务内容:"+taskS.getTaskContent());
//        out.println("<br>");
        out.println("现日期:"+time);
        out.println("<h3>进行中任务</h3>");


        for(int i=1;i<=nums;i++) {
            task_seek taskS = new task_seek(i);

            if(taskS.tip.equals("进行中")) {
                out.println("<br>");
                out.println("<center>");
                String TaskTheme1 = taskS.getTaskTheme();
                out.println("<a href=\"Doing?TaskTheme="+TaskTheme1+"\">"+i+"、"+TaskTheme1+"</a>");
                out.println("</center>");




//                out.println("</body>");
//                out.println("</html>");

            }




        }




        out.print("</form></div>");
        out.print("</body>\t<script>\n" +
                "\t\tvar canvas = document.querySelector('.rain');\n" +
                "\t\tvar ctx = canvas.getContext('2d')\n" +
                "\t\tvar aRain = [];\n" +
                "\n" +
                "\t\tvar w = window.innerWidth;\n" +
                "\t\tvar h = window.innerHeight;\n" +
                "\t\tcanvas.width = w;\n" +
                "\t\tcanvas.height = h;\n" +
                "\t\twindow.onresize = function() {\n" +
                "\t\t\tcanvas.width = window.innerWidth;\n" +
                "\t\t\tcanvas.height = window.innerHeight;\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tfunction random(min, max) {\n" +
                "\t\t\treturn Math.random() * (max - min) + min;\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tfunction Rain() {};\n" +
                "\t\tRain.prototype = {\n" +
                "\t\t\tinit: function() {\n" +
                "\t\t\t\tthis.x = random(0, w);\n" +
                "\t\t\t\tthis.y = 0;\n" +
                "\t\t\t\tthis.vy = random(4, 6);\n" +
                "\t\t\t\tthis.h = random(0.8 * h, 0.9 * h)\n" +
                "\t\t\t\tthis.r = 1;\n" +
                "\t\t\t\tthis.vr = 1;\n" +
                "\t\t\t\tthis.rr = random(60, 130);\n" +
                "\t\t\t\tthis.ready = true;\n" +
                "\t\t\t},\n" +
                "\t\t\tdraw: function() {\n" +
                "\t\t\t\tif(this.y < this.h && this.ready) {\n" +
                "\t\t\t\t\tctx.beginPath();\n" +
                "\t\t\t\t\tctx.fillStyle = '#31f7f7';\n" +
                "\t\t\t\t\tctx.fillRect(this.x, this.y, 4, 10);\n" +
                "\t\t\t\t} else {\n" +
                "\t\t\t\t\tctx.beginPath();\n" +
                "\t\t\t\t\tctx.strokeStyle = '#31f7f7';\n" +
                "\t\t\t\t\tctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);\n" +
                "\t\t\t\t\tctx.stroke();\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\tmove: function() {\n" +
                "\t\t\t\tif(!this.ready) {\n" +
                "\t\t\t\t\tif(this.r < 1) {\n" +
                "\t\t\t\t\t\tthis.ready = true;\n" +
                "\t\t\t\t\t} else {\n" +
                "\t\t\t\t\t\tthis.r--;\n" +
                "\t\t\t\t\t\tthis.draw();\n" +
                "\t\t\t\t\t\treturn;\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t}\n" +
                "\n" +
                "\t\t\t\tif(this.y < this.h) {\n" +
                "\t\t\t\t\tthis.y += this.vy;\n" +
                "\t\t\t\t} else {\n" +
                "\t\t\t\t\tthis.r += this.vr;\n" +
                "\t\t\t\t};\n" +
                "\t\t\t\tif(this.r < this.rr) {\n" +
                "\t\t\t\t\tthis.draw();\n" +
                "\t\t\t\t} else {\n" +
                "\t\t\t\t\tthis.init();\n" +
                "\t\t\t\t};\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tsetInterval(function() {\n" +
                "\t\t\tctx.fillStyle = 'rgba(0,0,0,0.05)';\n" +
                "\t\t\tctx.fillRect(0, 0, w, h);\n" +
                "\t\t\tfor(var item of aRain) {\n" +
                "\t\t\t\titem.move();\n" +
                "\t\t\t}\n" +
                "\t\t}, 1000 / 60)\n" +
                "\n" +
                "\t\tfunction creatRain(num) {\n" +
                "\t\t\tfor(var i = 0; i < num; i++) {\n" +
                "\t\t\t\tsetTimeout(function() {\n" +
                "\t\t\t\t\tvar rain = new Rain();\n" +
                "\t\t\t\t\taRain.push(rain);\n" +
                "\t\t\t\t\train.init();\n" +
                "\t\t\t\t\train.draw();\n" +
                "\t\t\t\t}, 200 * i)\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\n" +
                "\t\t//\t\tcreatRain(60);\n" +
                "\t\tcanvas.onmousedown = function(e) {\n" +
                "\t\t\tvar rain = new Rain();\n" +
                "\t\t\train.init();\n" +
                "\t\t\train.x = e.offsetX;\n" +
                "\t\t\train.y = e.offsetY;\n" +
                "\t\t\train.r = random(5, 30);\n" +
                "\t\t\train.ready = false;\n" +
                "\t\t\taRain.push(rain);\n" +
                "\t\t}</script></html>");


    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost( req,resp);
    }

}

